package com.wind.cqboot.plugin;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wind.cqboot.utils.HttpUtil;
import com.wind.cqboot.utils.MsgUtil;

import cn.hutool.core.util.StrUtil;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.utils.Msg;
import onebot.OnebotEvent;

@Component
public class MlRbootPlugin extends BotPlugin
{
    @Override
    public int onGroupMessage(@NotNull Bot bot,
            @NotNull OnebotEvent.GroupMessageEvent event)
    {
        String msg = event.getRawMessage();
        Long groupId = event.getGroupId();
        Long userId = event.getUserId();
        String atMsg = MsgUtil.getAtMsg(bot, event);
        if (null != atMsg)
        {
            if (StrUtil.isBlank(atMsg))
            {
                Msg.builder().at(userId).text("你得说点什么我才懂啊").sendToGroup(bot,
                        groupId);
            }
            else
            {
                String content = botMessageReply(atMsg);
                if (StrUtil.isNotBlank(content))
                {
                    Msg.builder().at(userId).text(content)
                            .reply(event.getMessageId())
                            .sendToGroup(bot, groupId);
                }
            }
            return MESSAGE_BLOCK;
        }
        // 继续执行下一个插件
        return MESSAGE_IGNORE;
    }

    @Value("${rebot.ml.apikey}")
    private String apikey;

    @Value("${rebot.ml.secret}")
    private String secret;

    public String botMessageReply(String msg)
    {
        String params = "question=" + msg + "&api_key=" + apikey
                + "&api_secret=" + secret;
        // 只实现了基础的 具体的请自行修改
        return bulid(msg, params);
    }

    private static String bulid(String content, String params)
    {
        String result = HttpUtil.sendGet("http://i.itpk.cn/api.php", params);
        if (StrUtil.isBlank(result)) return "茉莉机器人故障，请稍后再试";
        JsonObject json;
        StringBuffer sb = new StringBuffer();
        String text = "";
        if (content.contains("笑话"))
        {
            json = JsonParser.parseString(result).getAsJsonObject();
            sb.append("标题：" + json.get("title") + "\n");
            sb.append("内容：" + json.get("content").toString()
                    .replaceAll("\\\\r|\\\\n|&nbsp;", "\n"));
            text = sb.toString();
        }
        else
        {
            switch (content)
            {
                case "观音灵签":
                    json = JsonParser.parseString(result).getAsJsonObject();
                    sb.append("您抽取的是第" + json.get("number2") + "签\n");
                    sb.append("签位：" + json.get("haohua") + "\n");
                    sb.append("签语：" + json.get("qianyu") + "\n");
                    sb.append("诗意：" + json.get("shiyi") + "\n");
                    sb.append("解签：" + json.get("jieqian"));
                    text = sb.toString();
                    break;
                case "月老灵签":
                    json = JsonParser.parseString(result).getAsJsonObject();
                    sb.append("您抽取的是第" + json.get("number2") + "签\n");
                    sb.append("签位：" + json.get("haohua") + "\n");
                    sb.append("签语：" + json.get("shiyi") + "\n");
                    sb.append("解签：" + json.get("jieqian"));
                    text = sb.toString();
                    break;
                case "财神爷灵签":
                    json = JsonParser.parseString(result).getAsJsonObject();
                    sb.append("您抽取的是第" + json.get("number2") + "签\n");
                    sb.append("签语：" + json.get("qianyu") + "\n");
                    sb.append("注释：" + json.get("zhushi") + "\n");
                    sb.append("解签：" + json.get("jieqian") + "\n");
                    sb.append("解说：" + json.get("jieshuo") + "\n");
                    sb.append("婚姻：" + json.get("hunyin") + "\n");
                    sb.append("事业：" + json.get("shiye") + "\n");
                    sb.append("求财：" + json.get("qiucai"));
                    text = sb.toString();
                    break;
                case "指令":
                    text = mainMenu();
                    break;
                case "ip":
                    text = mainMenu();
                    break;
                default:
                    text = result;
                    break;
            }
        }
        return text;
    }

    public static String mainMenu()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("指令:\n");
        sb.append("地区天气 \n");
        sb.append("天气\n");
        sb.append("@tq您的ip地址\n");
        sb.append("@ip您的ip地址\n");
        // sb.append("ip\n");
        // sb.append("@qq要查询的QQ号码\n");
        // sb.append("@lol英雄联盟大区--角色名称\n");
        sb.append("@sfz身份证号码\n");
        sb.append("@sjh手机号码\n");
        sb.append("@cy四字成语\n");
        sb.append("笑话\n");
        sb.append("观音灵签\n");
        sb.append("月老灵签\n");
        sb.append("财神爷灵签");
        return sb.toString();
    }
}
